home *** CD-ROM | disk | FTP | other *** search
/ Mac Mania 6 / MacMania 6.toast / / Tools&Utilities / EnterAct Stuff / Drag_on Modules / hAWK programs / $ClipOldCDeclToNew < prev    next >
Text File  |  1995-01-23  |  3KB  |  90 lines

  1. # $OldCtoNewDaemon : convert old-style C function "lead in"
  2. # to new style. This is an example of a "clip daemon", also
  3. # known as the "magic clipboard".
  4. # Use: call up hAWK, and run this program: it will continue
  5. # to run concurrently as you return to your editor, monitoring
  6. # the clipboard all the while. When you Copy an old-style
  7. # C function declaration in your editor, this program will
  8. # use getclip() to retrieve it, generate a new-style declaration,
  9. # and then give the new one back to your editor with putclip().
  10. # A flash of the menu bar indicates success, at which point
  11. # you can paste the new decl over the old one.
  12.  
  13. # IMPORTANT! When you're done with this program (or any similar
  14. # program that never terminates on its own), 
  15. # PRESS <Command><period> in your editor to stop it.
  16. # (The stderr window will appear as confirmation that this program
  17. # has stopped -- you can ignore it otherwise.)
  18.  
  19. # Sequence in brief is Copy old style, flash, Paste new style.
  20. # When done changing declaration, press <Command><period>.
  21.  
  22. # This program needs no input, since it runs all the time
  23. # and looks to your clipboard for input. The only output it
  24. # generates is to your clipboard, also.
  25. # This is an example only, from which you should be able to
  26. # derive specific "magic clipboard" programs to suit your needs.
  27. # Here the old-style declaration must follow a rigid format:
  28. #    <space>return-type
  29. #    function-name(parameters)
  30. #    <tab>param-type param1;
  31. #    ...
  32. #    <tab>param-type last_param;
  33.  
  34.  
  35. BEGIN {
  36.     while (1) # run until <Command><period>...
  37.         {
  38.         # see if clipboard has changed
  39.         if ((newClip = getclip(32)) != oldClip)
  40.             {
  41.             oldClip = newClip;
  42.             # a leading space is used as the trigger
  43.             if (substr(newClip, 1,1) == " ")
  44.                 ClipFixProto()
  45.             }
  46.         }
  47.     }
  48.  
  49. function ClipFixProto(    elClip, out, numLines, lines, i)
  50.     {
  51.     elClip = getclip(); # gets calling editor's private clip
  52.     # split clip up into separate lines
  53.     numLines = split(elClip, lines, "\r");
  54.     # with "split", the last line will be empty if the last
  55.     # character copied was a return.
  56.     if (lines[numLines] == "")
  57.         --numLines;
  58.     out = "";
  59.     out = "pascal" lines[1]; #get return type, prepend "pascal"
  60.     sub(/\(.*\)/, "", lines[2]); #clear params after the name
  61.     out = out " " lines[2]; #get the function name
  62.     if (numLines >= 3)
  63.         sub(/[ \t]/, "", lines[3]);
  64.     for (i = 3; i <= numLines; ++i)
  65.         {
  66.         sub(/;/, ",", lines[i]); # change semicolons to commas
  67.         }
  68.     out = out "\r";
  69.     if (numLines >= 3)
  70.         {
  71.         # small bug, must be only one comma on last line of parms
  72.         sub(/,/, "", lines[numLines]); #delete last comma
  73.         # get the (new-style) parameters
  74.         if (numLines == 3)
  75.             out = out "\t(" lines[3] ")\r"
  76.         else if (numLines > 3)
  77.             out = out "\t(" lines[3] "\r"
  78.         for (i = 4; i < numLines; ++i)
  79.             out = out lines[i] "\r";
  80.         if (numLines > 3)
  81.             out = out lines[numLines] ")\r"
  82.         }
  83.     # send the result back to the calling editor's clip
  84.     putclip(out);
  85.     ## test only
  86.     ##print out;
  87.     # flash menu bar to signal something happened
  88.     beep(0);
  89.     }
  90.